home *** CD-ROM | disk | FTP | other *** search
- #include "Sample.h"
- #include "MPDialogs.h"
- #include "DD.h"
- #include "SampleProcs.h"
-
- /* Sample application demonstrating the use of the multi-pane dialog code
- * Largly borrowed from the Sample Drag application in the
- * Macintosh Drag and Drop package.
- */
-
- // Comment out to use default Action Procedures.
- #define CUSTOM_PROCS
-
- // Define this to use a modal dialog
- #define MODAL
-
- // Menu constants
- #define kAppleMenuID 128
- #define kAboutMenuItem 1
- #define kFileMenuID 129
- #define kPrefsItem 1
- #define kDisplayItem 2
- #define kCloseItem 4
- #define kQuitMenuItem 6
- #define kAboutDLOG 128
- #define kPrefDLOG 200
-
- // Global variables
- Boolean gQuitFlag; // true when the app should exit the main event loop
- DialogPtr gAbout = NULL; // modeless About dialog box
-
- MenuHandle gAppleMenuHandle, gFileMenuHandle; // For our menus
- DialogPtr prefDlog = NULL; // The multi-pane dialog
- Handle thePrefs = NULL; // Storage of the data entered in the dialog
-
- // UPPs for the custom Action Procedures
- #ifdef CUSTOM_PROCS
- ClickActionUPP myClickAction = NULL;
- EditActionUPP myEditAction = NULL;
- #endif
-
- /***************************************************
- * Typical Mac toolbox routines
- ***************************************************/
-
- // CreateMenus makes menus the old-fashioned way
- void CreateMenus()
- {
- // create Apple menu
- gAppleMenuHandle = GetMenu(kAppleMenuID);
- AddResMenu(gAppleMenuHandle, 'DRVR');
- InsertMenu(gAppleMenuHandle, 0);
-
- // create File menu
- gFileMenuHandle = GetMenu(kFileMenuID);
- InsertMenu(gFileMenuHandle, 0);
-
- DrawMenuBar();
- }
-
- // DoMenuCommand handles user menu selections
- void DoMenuCommand(long menuVal)
- {
- short theItem, theMenu;
- Str255 deskAccessoryName;
- WindowPtr whichWindow;
-
- theItem = LoWord(menuVal);
- theMenu = HiWord(menuVal);
-
- switch (theMenu) {
-
- case kAppleMenuID:
- if (theItem == kAboutMenuItem) {
- if (gAbout) SelectWindow(gAbout);
- else gAbout = GetNewDialog(kAboutDLOG, NULL, (WindowPtr) -1);
- } else {
- GetItem(gAppleMenuHandle, theItem, deskAccessoryName);
- (void) OpenDeskAcc(deskAccessoryName);
- }
- break;
-
- case kFileMenuID:
-
- switch (theItem) {
- case kPrefsItem:
- if (!prefDlog) {
- prefDlog = OpenMPDialog(kPrefDLOG, NULL,
- #ifdef CUSTOM_PROCS
- myClickAction, myEditAction, NULL, &thePrefs);
- #else
- NULL, NULL, NULL, &thePrefs);
- #endif
- }
- if (prefDlog) SetMenusBusy();
- break;
- case kDisplayItem:
- DialogDisplay(thePrefs);
- break;
- case kCloseItem:
- if (!(whichWindow = FrontWindow())) break;
- if (whichWindow == gAbout) {
- DisposeDialog(gAbout);
- gAbout = NULL;
- } else if (whichWindow == prefDlog) {
- CloseMPDialog(&prefDlog);
- if (!prefDlog) SetMenusIdle(); // Dialog has closed
- }
- break;
- case kQuitMenuItem:
- gQuitFlag = true;
- break;
- }
- break;
-
- }
- HiliteMenu(0); // unhilight menu title
- }
-
- // Dim menus while movable modal dialog is displayed
- void SetMenusBusy()
- {
- #ifdef MODAL
- DisableItem(gAppleMenuHandle, 0);
- DisableItem(gFileMenuHandle, 0);
- #else
- DisableItem(gFileMenuHandle, kPrefsItem);
- #endif
- DrawMenuBar();
- }
-
- // Display all menu options
- void SetMenusIdle()
- {
- #ifdef MODAL
- EnableItem(gAppleMenuHandle, 0);
- EnableItem(gFileMenuHandle, 0);
- #else
- EnableItem(gFileMenuHandle, kPrefsItem);
- #endif
- DrawMenuBar();
- }
-
- /***************************************************
- * finally, the main program and event loop
- ***************************************************/
-
- void main()
- {
- EventRecord mainEventRec;
- Boolean eventFlag;
- WindowPtr whichWindow;
-
- // initialize the toolbox
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0);
- InitCursor();
-
- FlushEvents(everyEvent,0);
- MaxApplZone();
-
- // initialize application global
- gQuitFlag = false;
-
- // make the menus
- CreateMenus();
-
- // initialize universal procedure pointers
- #ifdef CUSTOM_PROCS
- myClickAction = NewClickActionProc(MyClickAction);
- myEditAction = NewEditActionProc(MyEditAction);
- #endif
-
- // main event loop
- while (!gQuitFlag) {
-
- eventFlag = WaitNextEvent(everyEvent, &mainEventRec, 60*60*60, nil);
-
- // Inline version of AdjustMenus
- AbleDisItem(kFileMenuID, kDisplayItem, thePrefs != NULL);
-
- // Let multi-pane dialog have a go at it first
- if (DoMPDialogEvent(&prefDlog, &mainEventRec)) {
- if (!prefDlog) SetMenusIdle(); // Dialog has closed
- continue;
- }
-
- // Handle our About dialog box
- if (IsDialogEvent(&mainEventRec)) {
- DialogPtr theDialog;
- short itemHit;
-
- if (DialogSelect(&mainEventRec, &theDialog, &itemHit))
- continue;
- }
-
- // The multi-pane dialog code didn't handle it, we have to.
- switch(mainEventRec.what) {
-
- case mouseDown:
- switch (FindWindow(mainEventRec.where, &whichWindow)) {
-
- case inSysWindow: // desk accessory window
- SystemClick(&mainEventRec, whichWindow);
- break;
-
- case inMenuBar:
- AbleDisItem(kFileMenuID, kCloseItem, FrontWindow() != NULL);
- DoMenuCommand(MenuSelect(mainEventRec.where));
- break;
-
- case inContent:
- #ifdef MODAL
- if (!prefDlog) {
- #endif
- if (whichWindow != FrontWindow()) {
- SelectWindow(whichWindow);
- }
- #ifdef MODAL
- } else SysBeep(2);
- #endif
- break;
-
- case inGoAway:
- #ifdef MODAL
- if (!prefDlog) {
- #endif
- if (gAbout == whichWindow &&
- TrackGoAway(whichWindow, mainEventRec.where)) {
- DisposeDialog(gAbout);
- gAbout = NULL;
- }
- #ifdef MODAL
- } else SysBeep(2);
- #endif
-
- case inDrag:
- #ifdef MODAL
- if (!prefDlog) {
- #endif
- DragWindow(whichWindow, mainEventRec.where, &qd.screenBits.bounds);
- #ifdef MODAL
- } else SysBeep(2);
- #endif
- break;
-
- default:
- // This application really doesn't do very much.
- break;
- }
- break;
-
- case keyDown:
- case autoKey:
- if (mainEventRec.modifiers & cmdKey) {
- long mk = MenuKey(mainEventRec.message & charCodeMask);
- if (mk) {
- DoMenuCommand(mk);
- break;
- }
- }
- break;
-
- // a real app should handle all of these events.
- // Since the code is in Inside Mac:Mac Toolbox Essentials,
- // you really have no excuse for not supporting all the
- // standard low-level events
- // however, this is just a sample program, so I have an excuse
-
- case updateEvt:
- case activateEvt:
- case osEvt:
- case diskEvt:
- case mouseUp:
- break;
- }
- }
-
- // Good night
- }
-